/**
 * 订单管理js
 */
define(function(require, exports, module){
	
	require('dialog');
	require('wdate');
	require('datatables');
	module.exports={
			
			//公共提示框
			msg:function(msg){
				var d = dialog({
					title:"提示",
					content:msg,
					ok:function(){},
					okValue:"确定"
				})
				d.width(320);
				d.showModal();
			},
		
			//添加订单客户信息
			save_order:function(){
				$('#save_2').click(function(){
					
					var order_man = $("#order_man").val();
					if(order_man ==''){
						module.exports.msg('客户名称不能为空');
						return false;
					}
					var man_phone = $("#man_phone").val();
					if(man_phone ==''){
						module.exports.msg('手机号不能为空');
						return false;
					}
					var order_info = $("#order_info").val();
					var receptionist = $("#receptionist").val();
					if(receptionist == ''){
						module.exports.msg('接待人不能为空');
						return false;
					}
					var venue_ids = [];
					$('input:checked').each(function(k,v){
						venue_ids.push($(v).val())
					})
					if(venue_ids == ''){
						module.exports.msg('请选择场馆');
						return false;
					}
					var order_type = $('#order_type').val();
					if(order_type == ''){
						module.exports.msg('请选择婚宴类型');
						return false;
					}
					var g_time = $('#g_time').val();
					if(g_time ==''){
						module.exports.msg('请选择公历时间');
						return false;
					}
					var n_time = $('#n_time').val();
					if(n_time ==''){
						$('#n_time').val('计算中....');
						module.exports.msg('请等待系统计算农历时间');
						return false;
					}
					var week = $('#week').val();
					var start_time = $("#start_time").val();
					var bargain_money = parseFloat($('#bargain_money').val());
					if(bargain_money < 0){
						module.exports.msg('定金不能小于0');
						return false;
					}
                    var data = {'order_man':order_man,'man_phone':man_phone,'order_info':order_info,'receptionist':receptionist,'place_id':venue_ids,'order_type':order_type,'g_time':g_time,'n_time':n_time,'week':week,'start_time':start_time,'bargain_money':bargain_money};
					$.post('/orders/addorderinfo',data,function(data){
						if(data.code == 1){
							$('#order_id').val(data.id); 
                            $('#order_num').val(data.order_num);
                            module.exports.msg('添加订单信息成功');
                          //隐藏第一个div,显示第二个div
							$('#one').attr('style','display:none');
							$('#two').attr('style','display:block');
							$(this).attr('style','display:none');
						}else{
							module.exports.msg(data.msg);
						}
					});
				});
			},
			
			edit_order:function(){
				$('#edit_order').click(function(){
					var id = $('#order_id').val();
					var order_man = $("#order_man").val();
					if(order_man ==''){
						module.exports.msg('客户名称不能为空');
						return false;
					}
					var man_phone = $("#man_phone").val();
					if(man_phone ==''){
						module.exports.msg('手机号不能为空');
						return false;
					}
					var order_info = $("#order_info").val();
					var receptionist = $("#receptionist").val();
					var venue_ids = [];
					$('input:checked').each(function(k,v){
						venue_ids.push($(v).val())
					})
					if(venue_ids == ''){
						module.exports.msg('请选择场馆');
						return false;
					}
					var order_type = $('#order_type').val();
					if(order_type == ''){
						module.exports.msg('请选择婚宴类型');
						return false;
					}
					var g_time = $('#g_time').val();
					if(g_time ==''){
						module.exports.msg('请选择公历时间');
						return false;
					}
					var n_time = $('#n_time').val();
					var week = $('#week').val();
					var start_time = $("#start_time").val();
					var bargain_money = parseFloat($('#bargain_money').val());
					if(bargain_money < 0){
						module.exports.msg('定金不能小于0');
						return false;
					}
                    var data = {'id':id,'order_man':order_man,'man_phone':man_phone,'order_info':order_info,'receptionist':receptionist,'place_id':venue_ids,'order_type':order_type,'g_time':g_time,'n_time':n_time,'week':week,'start_time':start_time,'bargain_money':bargain_money};
					$.post('/orders/editorderinfo',data,function(data){
						if(data.code == 1){
                            module.exports.msg('修改成功');
                            //修改预约金后自动更新合计的信息
                            var total_price = 0;
							$('.price_').each(function(k,v){
								total_price+=parseFloat($(v).attr('data'));
							})
							var s = parseFloat(total_price);
							var f = parseFloat($('#free').val());
							var f = f ? f:0;
							var b = parseFloat($('#bargain_money').val())
							var b = b ? b:0;
							$('#total').html(total_price);
							$('#need').html(s - f - b);
						
						    //隐藏第一个div,显示第二个div
							$('#one').attr('style','display:none');
							$('#two').attr('style','display:block');
						}else{
							module.exports.msg(data.msg);
						}
					});
				})
			},
			
			add_goods:function(){
				$('#add_goods').click(function(){
					if($('#order_num').val() == ''){
						module.exports.msg('请先提交订单信息');
						return false;
					}
					var id = $('#order_id').val();
					var order_num = $('#order_num').val()	
					var foods_id = $('#goods').attr('data');
					var unit_price = $('#unit_price').val();
					if(unit_price == ''){
						module.exports.msg('请选择商品');
						return false;
					}
					var num = $('#num').val();
					if(num <=0){
						module.exports.msg('请填写数量或数量必须大于0');
						return false;
					}
					var foods_name = $('#cn_name').val();
					var price = parseFloat(unit_price)*parseFloat(num);
					var data = {'order_id':id,'order_num':order_num,'foods_id':foods_id,'num':num};
					$.post('/orders/add_goods',data,function(m){
						if(m.code == 1){
							html ="";
							html += '<tr id="t_'+m.id+'">';
							html += '<td>'+m.id+'</td>';
							html += '<td>'+m.name+'</td>';
							html += '<td>'+foods_name+'</td>';
							html += '<td>'+unit_price+'</td>';
							html += '<td>'+num+'</td>';
							html += '<td class="price_" data="'+price+'">'+price+'</td>';
							html += '<td><a id_class="del" data="'+m.id+'" class="btn btn-danger">移除</a></td>';
							html += '<tr>';
							$('#tbody').append(html);
							
							var total_price = 0;
							$('.price_').each(function(k,v){
								total_price+=parseFloat($(v).attr('data'));
							})
							var s = parseFloat(total_price);
							var f = parseFloat($('#free').val());
							var f = f ? f:0;
							var b = parseFloat($('#bargain_money').val())
							var b = b ? b:0;
							$('#total').html(total_price);
							$('#need').html(s - f - b);
						}else{
							module.exports.msg(m);
						}
					});
					
				})
			},
			
			finsh:function(){
				
				$(document).on('click','#save_3', function(){

					var id = $('#order_id').val();
					if(id == ''){
						module.exports.msg('请先添加客户');
						return false;
					}
					if($('#order_num').val() == ''){
						module.exports.msg('请先提交订单信息');
						return false;
					}
					var status = $('#status').val();
					if(status == ''){
						module.exports.msg('请选择状态');
						return false;
					}
					var free = $('#free').val();
					var total_price = $('#total').html();
					$.post('/orders/finsh',{'id':id,'status':status,'free':free,'total_price':total_price},function(data){
						if(data == 1){
							var d = dialog({
								title:"提示",
								content:'操作成功',
								ok:function(){
									window.location.href='/orders';
								},
								okValue:"确定"
							})
							d.width(320);
							d.showModal();
						}else{
							module.exports.msg(data);
						}
					})
				})
			},
			
			//移除已经添加的商品
			del:function(){
				$(document).on('click', "a[id_class='del']", function(){
					var id = $(this).attr('data');
					$.post('/orders/del_goods',{'id':id},function(data){
						if(data == 1){
							$('#t_'+id).remove();
							var total_price = 0;
							$('.price_').each(function(k,v){
								total_price+=parseFloat($(v).attr('data'));
							})
							var s = parseFloat(total_price);
							var f = parseFloat($('#free').val());
							var f = f ? f:0;
							var b = parseFloat($('#bargain_money').val())
							var b = b ? b:0;
							$('#total').html(total_price);
							$('#need').html(s - f - b);
						}else{
							module.exports.msg(data);
						}
					})
				})
			},
			
			//修改商品数量
			edit:function(){
				$("input[name='num']").blur(function(){
					var id = $(this).attr('data');
				    //获得单价，数量
				    var unit_price = $('#unit_price_'+id).val();
				    var num = parseInt($('#num_'+id).val());
				    if(num <= 0){
				    	module.exports.msg('数量必须大于等于1');
				    	return false;
				    }
				    $('#price_'+id).val(parseFloat(unit_price)*parseFloat(num));
				});
				//点击修改
				$(document).on('click', "a[id_class='edit']", function(){
					var id = $(this).attr('data');
					var unit_price = $('#unit_price_'+id).val();
				    var num = $('#num_'+id).val();
				    var price = $(this).attr('data');
				    var s_price = parseFloat(unit_price)*parseFloat(num);
				    $.post('/orders/edit_goods',{'id':id,'num':num,'unit_price':unit_price,'price':s_price},function(data){
				    	if(data == 1){
				    		var total_price = 0;
							$('td.price_ input[name="price"]').each(function(k,v){
								total_price+=parseFloat($(v).val());
							})
							var s = parseFloat(total_price);
							var f = parseFloat($('#free').val());
							var f = f ? f:0;
							var b = parseFloat($('#bargain_money').val())
							var b = b ? b:0;
							$('#total').html(total_price);
							$('td#need').html(s - f - b);
				    	}else{
				    		module.exports.msg(data);
				    	}
				    })
				})
			},
			
			erji:function(){
				$('#class_id').change(function(){
					var class_id = $(this).val();
					$.get('/order/erji',{'class_id':class_id},function(data){
						if(data.code == 1){
							$('#goods').html='';
							var html = '<option value="">请选择商品</option>';
							$.each(data.arr,function(k,v){
								html += '<option value="'+v.id+'">'+v.cn_name+'</option>';
							});
							$("#goods").html(html);
						}
					})
				});
				//选中商品后查询价格
				$('#goods').change(function(){
					var id = $('#goods').val();
					$(this).attr('data',id);
					$.get('/orders/get_price_by_id',{'id':id},function(data){
						if(data){
							$('#unit_price').val(data.price);
							$('#cn_name').val(data.cn_name);
						}
					})
				})
			},
			
			//计算需要支付的价格
			need:function(){
				$('#free').blur(function(){
					if(!$(this).val()){
						$(this).val(0);
					}
					var b = parseFloat($('#bargain_money').val());
					var b = b ? b:0;
					$('#need').html( parseFloat($('#total').html()) - parseFloat($(this).val()) - b );
				})
			},
			
			autoadd:function(){
				$("#num").blur(function(){
					  $('#price').attr('value', parseFloat($('#unit_price').val()) * parseFloat($('#num').val()));
				});
			},
			
			//时间选择器
			datepick:function(){
				$(".Wdate").focus(function(){
					if(arguments[0] && arguments[1]){
						WdatePicker({dateFmt:'yyyy-MM-dd', minDate:arguments[0], maxDate:arguments[1]})
					}else{
						WdatePicker({dateFmt:'yyyy-MM-dd'});
					}
					var date = $(this).val().split("-");
		            //计算农历
					if(date.length > 1){
						var link = 'year='+date[0]+'&month='+date[1]+'&day='+date[2];
						$.get('/publicservice/solartolunar?'+link, function(data){
							$('.lunardate').val(data.lunar_time.lunar_time)
							$('.week').val(data.lunar_time.week);
						})
						
					}
		            
		        });
				$("#start_time").focus(function(){
					//var date = new Date();
					//$(this).val(date.getFullYear()+'-'+date.getMonth()+'-'+date.getDate());
		            WdatePicker({dateFmt:'HH:mm'})
		        });
			},
			
			open_close:function(){
				$('#open_close').on('click', function(){
					if($(this).attr('data') == 1){
						$('#one').attr('style','display:none');
						$(this).html('展开订单基本信息');
						$(this).attr('data',0);
						return false;
					}
					$(this).attr('data',1);
					$(this).html('收起订单基本信息');
					$('#one').attr('style','display:block');
					$('#two').attr('style','display:block');
					$('#save_2').attr('style','display:none');
				})
			}
			
			
			
	}
})
